package org.ala.tiktools.generator.mybatis.dbinfo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 索引数据
 *
 * @author ala
 * @date 2025-03-14 09:28
 */
public class IndexInfo {
    /**
     *  主键索引
     */
    public static final String NAME_PK = "PRIMARY";
    /**
     *  所属表名
     */
    protected String table;
    /**
     *  索引名称
     */
    protected String name;
    /**
     *  是否唯一索引
     */
    protected boolean unique;
    /**
     *  字段名
     */
    protected List<Field> columns;
    /**
     *  升序还是降序
     */
    protected boolean asc;
    /**
     *  如何被压缩
     */
    protected String packed;
    /**
     *  索引的类型
     *  <p> BTREE, FULLTEXT, HASH, RTREE
     */
    protected String indexType;
    /**
     *  索引的额外信息
     */
    protected String comment;
    /**
     *  注释
     */
    protected String indexComment;

    public static IndexInfo build() {return new IndexInfo();}

    /**
     *  转换成createIndex
     */
    public String toSql() {
        String columns = this.columns.stream().map(Field::toSql).collect(Collectors.joining(", "));
        String sql = String.format("CREATE %s INDEX %s ON %s(%s) USING %s",
                unique ? "UNIQUE" : "",
                name,
                table,
                columns,
                indexType
                );
        if (comment != null) {
            sql += String.format(" COMMENT '%s'", comment);
        }
        sql += ";";
        return sql;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public IndexInfo name(String name) {
        setName(name);
        return this;
    }

    public boolean isUnique() {
        return unique;
    }

    public void setUnique(boolean unique) {
        this.unique = unique;
    }

    public IndexInfo unique(boolean unique) {
        setUnique(unique);
        return this;
    }

    public List<Field> getColumns() {
        return columns;
    }

    public void setColumns(List<Field> columns) {
        this.columns = columns;
    }

    public IndexInfo columns(List<Field> columns) {
        setColumns(columns);
        return this;
    }

    public IndexInfo appendColumn(Field column) {
        getColumns().add(column);
        return this;
    }

    public boolean isAsc() {
        return asc;
    }

    public void setAsc(boolean asc) {
        this.asc = asc;
    }

    public IndexInfo asc(boolean asc) {
        setAsc(asc);
        return this;
    }

    public String getPacked() {
        return packed;
    }

    public void setPacked(String packed) {
        this.packed = packed;
    }

    public IndexInfo packed(String packed) {
        setPacked(packed);
        return this;
    }

    public String getIndexType() {
        return indexType;
    }

    public void setIndexType(String indexType) {
        this.indexType = indexType;
    }

    public IndexInfo indexType(String indexType) {
        setIndexType(indexType);
        return this;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public IndexInfo comment(String comment) {
        setComment(comment);
        return this;
    }

    public String getIndexComment() {
        return indexComment;
    }

    public void setIndexComment(String indexComment) {
        this.indexComment = indexComment;
    }

    public IndexInfo indexComment(String indexComment) {
        setIndexComment(indexComment);
        return this;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public IndexInfo table(String table) {
        setTable(table);
        return this;
    }

    public static class Field {
        String column;
        boolean asc;

        public static Field build(String column, boolean asc) {
            Field f = new Field();
            f.column = column;
            f.asc = asc;
            return f;
        }

        public String toSql() {
            return String.format("%s %s", column, asc ? "ASC" : "DESC");
        }

        public String getColumn() {
            return column;
        }

        public void setColumn(String column) {
            this.column = column;
        }

        public boolean isAsc() {
            return asc;
        }

        public void setAsc(boolean asc) {
            this.asc = asc;
        }
    }
}
